﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ARS.Control.Common.EntityManage;

namespace ARS.Control.Custom.Entity.Forms
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:Form runat=server></{0}:Form>")]
    public class Form : WebControl
    {


        public Form()
        {
            this.Field = new Fields.Field();
            this.IsEntity = true;
        }

        public Form(bool isEntity)
        {
            this.Field = new Fields.Field();
            this.IsEntity = isEntity;
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Text
        {
            get
            {
                String s = (String)ViewState["Text"];
                return ((s == null) ? String.Empty : s);
            }

            set
            {
                ViewState["Text"] = value;
            }
        }
        /// <summary>
        /// 标识是不是实体的时候
        /// </summary>
        public bool IsEntity
        {
            get;
            set;
        }

        public Fields.Field Field
        {
            get;
            set;
        }
        /// <summary>
        /// 获取行的代码
        /// </summary>
        /// <returns></returns>
        protected virtual string GetTrHtml()
        {
            if (IsEntity)
            {
                return string.Format(TrHtml, Field.Verbose_Name, Field.Help_Text);

            }
            else
            {
                return "{0}";
            }
            
            //return string.Format("<tr><td>{0}</td><td>{{0}}{1}</td></tr>",Field.Verbose_Name,Field.Help_Text);
        }

        public const string EntityModifyPanel_Tr = "EntityModifyPanel_Tr";

        protected string TrHtml
        {
            get
            {
                string value = ARS.PublicTool.WebHelper.DataCache.GetCache<string>(EntityModifyPanel_Tr);
                if (value == null)
                {
                    value = "<tr><td>{0}</td><td>{{0}}{1}</td></tr>";
                }
                return value;
            }
        }

        public static void SetTrHml(string html)
        {
            ARS.PublicTool.WebHelper.DataCache.SetCache(EntityModifyPanel_Tr, html);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            //this.Visible = false;
            if (Field.Visible)
            {
                base.Render(writer);
            }            
        }

        protected override void RenderContents(HtmlTextWriter output)
        {
            //this.Visible = false;
            //output.Write(Text);
        }

        //public virtual void RegisterClientScriptResource(Page page, Type type)
        //{

        //}

        /// <summary>
        ///  获取输入值
        /// </summary>
        /// <returns></returns>
        public virtual object GetInputValue()
        {
            return HttpContext.Current.Request.Form[Field.Html_Control_Name];
            //return null;
        }

        public virtual void SetInputValue(EntityKeyInfo keyInfo)
        {
            
        }
    }
}
